<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="query.search" clearable @clear="getGoods">
            <el-button slot="append" icon="el-icon-search" @click="getGoods"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="$router.push('/addgood')">添加</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodslist" stripe border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量(kg)" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" prop="add_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="editGoodOpen(scope.row)"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="delGood(scope.row._id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[2, 3, 4, 5]"
      :page-size="query.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" background>
    </el-pagination>
    </el-card>


    <el-dialog
    title="修改商品"
    :visible.sync="editdialogVisible"
    width="50%">
    <el-form :model="editForm" :rules="editRules" ref="editRef">
      <el-form-item label="商品名称" prop="goods_name" label-width="100px">
        <el-input v-model="editForm.goods_name"></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="goods_price" label-width="100px">
        <el-input v-model="editForm.goods_price" type="number"></el-input>
      </el-form-item>
      <el-form-item label="商品重量" prop="goods_weight" label-width="100px">
        <el-input v-model="editForm.goods_weight"></el-input>
      </el-form-item>
      <el-form-item label="商品数量" prop="goods_number" label-width="100px">
        <el-input v-model="editForm.goods_number" type="number"></el-input>
      </el-form-item>
      <el-form-item label="缩略图" label-width="100px">
        <el-upload :action="uploadAction" list-type="picture-card" :headers="{authorization: token}" :on-success="uploadSuccessHandle">+</el-upload>
      </el-form-item>
      <el-form-item label="修改时间" prop="upd_time" label-width="100px">
        <el-date-picker
        v-model="editForm.upd_time"
        type="datetime"
        placeholder="选择更新时间">
      </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="editdialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="editGood">确 定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
import {mapState, mapActions} from 'vuex'
export default {
  data(){
    return{
      query:{
        search:'',
        page: 1,
        pagesize: 2
      },
      // 控制修改对话框打开关闭
      editdialogVisible: false,
      // 修改表单对象
      editForm:{},
      // 上传地址
      uploadAction: 'http://localhost:3000/goods/upload',
      // 上传图片时的token
      token : '' || window.sessionStorage.getItem('token'),
      // 修改表单验证
      editRules: {
        goods_name:[{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        goods_number:[{ required: true, message: '请输入商品数量', trigger: 'blur' }],
        goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        goods_weight: [{ required: true, message: '请输入商品重量', trigger: 'blur' }],
        upd_time:[{ required: true, message: '请选择修改日期', trigger: 'blur' }]
      },
    }
  },
  created(){
    this.getGoods()
  },
  computed:{
    ...mapState('goodsAbout',['goodslist','total'])
  },
  methods:{
    ...mapActions('goodsAbout',['getGoodsData']),
    // 展示
    getGoods(){
      this.getGoodsData(this.query)
    },
    // 每页多少条发生变化触发
    handleSizeChange(newSize){
      this.query.pagesize = newSize
      this.query.page = 1
      this.getGoods()
    },
    // 页码发生变化触发
    handleCurrentChange(newPage){
      this.query.page = newPage
      this.getGoods()
    },
    // 删除商品
    delGood(id){
      this.$confirm('此操作将删除该品牌, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then( async () => {
          let {data} = await this.$http.delete('/goods/delgood/'+id)
          if (data.status !== 204) return this.$message.error('删除失败!')
          this.$message.success('删除成功!')
          this.getGoods()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 打开修改对话框
    async editGoodOpen(row){
      let {_id, goods_name, goods_price, goods_weight, goods_number, upd_time, goods_small_logo} = row
      this.editForm = {_id, goods_name, goods_price, goods_weight, goods_number, upd_time, goods_small_logo}
      this.editdialogVisible = true
    },
    // 上传图片成功回调
    uploadSuccessHandle(res){
      this.editForm.goods_small_logo = res
    },
    // 修改
    editGood(){
      this.$refs.editRef.validate(async valid => {
        if(valid){
          let _id = this.editForm._id
          delete this.editForm._id
          let {data} = await this.$http.patch('/goods/editgood/'+_id, this.editForm)
          if (data.status!== 201){
            this.$message.error('修改商品失败!')
            this.editdialogVisible = false
          }else{
            this.$message.success('修改商品成功!')
            this.getGoods()
            this.editdialogVisible = false
          }
        }
      })
    }


  }
}
</script>

<style>

</style>